package com.cl.mypureframe.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.cl.mypureframe.BaseApplication;
import com.cl.mypureframe.R;
import com.cl.mypureframe.adapter.GuideViewAdapter;
import com.cl.mypureframe.util.Utils;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.autosize.internal.CustomAdapt;

/**
 * @Class: GuideActivity
 * @Description: 引导页
 * @Author: ChenLei
 * @Date: 2020/11/18---16:48
 */
public class GuideActivity extends BaseActivity  {
    private static final int POINT_WIDTHHEIGHT = 10;// indicator小圆点的宽高
    private static final int POINT_MARGIN = 10;// indicator小圆点的间距
    // 引导页图片
    private final int[] imageViews = {R.drawable.shape_guide_one, R.drawable.shape_guide_two,
            R.drawable.shape_guide_three, R.drawable.shape_guide_four};
    // indicator图片
    private final int[] pointImageViews = {R.drawable.shape_ring_gray, R.drawable.shape_ring_blue};
    private LinearLayout llPoint;
    private boolean isLastPage;
    private boolean isDragPage;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        ViewPager viewPage = (ViewPager) findViewById(R.id.vp_main);
        llPoint = (LinearLayout) findViewById(R.id.llPoint);

        List<View> list = new ArrayList<>();
        // 将imageview添加到view
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams pointParams = new LinearLayout.LayoutParams(
                Utils.dip2px(this, POINT_WIDTHHEIGHT), Utils.dip2px(this, POINT_WIDTHHEIGHT));
        for (int i = 0; i < imageViews.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imageViews[i]);
            list.add(iv);

            // 根据图片多少，添加多少小圆点
            ImageView ivpoint = new ImageView(this);
            pointParams.setMargins(POINT_MARGIN, 0, 0, 0);
            ivpoint.setBackgroundResource(i < 1 ? pointImageViews[1] : pointImageViews[0]);
            ivpoint.setLayoutParams(pointParams);
            llPoint.addView(ivpoint);
        }
        // 加入适配器
        viewPage.setAdapter(new GuideViewAdapter(list));

        // 监听当前显示的页面，将对应的小圆点设置为选中状态，其它设置为未选中状态
        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                monitorPoint(position);
                isLastPage = position == imageViews.length - 1;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                if (isLastPage && isDragPage && arg2 == 0) {
                    onButtonClick(null);
                }
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                isDragPage = arg0 == ViewPager.SCROLL_STATE_DRAGGING;
            }
        });

    }

    /**
     * 判断小圆点
     *
     * @param position position
     */
    private void monitorPoint(int position) {
        for (int i = 0; i < imageViews.length; i++) {
            llPoint.getChildAt(i).setBackgroundResource(pointImageViews[0]);
        }
        llPoint.getChildAt(position).setBackgroundResource(pointImageViews[1]);

        // 当滑动到最后一个添加按钮点击进入，
        setVisable(position == imageViews.length - 1, R.id.btn_go);
    }

    @Override
    public void onButtonClick(View view) {
        BaseApplication.basePreferences.saveIsGuide(true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        startVerifyActivity(LoginActivity.class);
        finish();
    }

}
